package com.happok.onlineclass.usermanagement.feign.service;

import com.happok.common.exception.FeginException;
import com.happok.common.result.CommonResult;
import com.happok.microgateway.service.RoutingDelegate;
import com.happok.onlineclass.usermanagement.common.dto.ResourceReferenceDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.net.URI;
import java.net.URISyntaxException;

/**
 * 接口分类                        yapi 分类文件夹
 * 功能说明
 *
 * @author : chenac
 * @module 项目名称/模块名          yapi 项目组/子项目
 * @date : 2021/7/16 14:11
 */
@Service
public class ResourceCenterServiceImpl implements ResourceCenterService {

    @Autowired
    private RoutingDelegate routingDelegate;

    @Override
    public void addResourcesReference(String resIds) throws URISyntaxException {
        ResourceReferenceDTO resourceReferenceDTO = new ResourceReferenceDTO();
        resourceReferenceDTO.setResIds(resIds);
        RequestEntity requestEntity = new RequestEntity(resourceReferenceDTO, HttpMethod.POST, new URI("http://resource-center/resources/v1"));
        ResponseEntity<CommonResult> resultResponseEntity = routingDelegate.forward(requestEntity);
        if (!resultResponseEntity.getStatusCode().is2xxSuccessful()) {
            throw new FeginException(resultResponseEntity.getBody().getMsg());
        }
    }

    @Override
    public void deleteResources(String resIds) throws URISyntaxException {
        RequestEntity requestEntity = new RequestEntity(HttpMethod.DELETE, new URI("http://resource-center/resources/v1/" + resIds));
        ResponseEntity<CommonResult> resultResponseEntity = routingDelegate.forward(requestEntity);
        if (!resultResponseEntity.getStatusCode().is2xxSuccessful()) {
            throw new FeginException(resultResponseEntity.getBody().getMsg());
        }
    }
}